package com.google.coffeeshop.gui.desktop.orderpayment.seatdetail;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseFrame;
import com.google.coffeeshop.gui.desktop.SwingFactory;
import com.google.coffeeshop.gui.desktop.message.CaptionMessages;
import com.google.coffeeshop.gui.desktop.orderpayment.SeatBoardPanel;
import com.google.coffeeshop.gui.desktop.orderpayment.invoiceexport.PaymentFrame;

public class SeatDetailFrame extends AbstractBaseFrame {
	private static final long serialVersionUID = 1L;
	
	static public final int FILTER_VIEW = 0;
	static public final int MENU_VIEW = 1;
	private MenuProductPanel pnlMenuContent;
	private JSplitPane splpnlContent;
	private SeatDetailPanel pnlSeatDetail;
	private JScrollPane pnlScrollMenuContent;
	private JScrollPane pnlScrollSeatFilter;
	private SeatBoardPanel parent;
	private SeatDTO seat = null;
	private Long employeeId;
	
	
	public SeatDTO getSeat() {
		return seat;
	}

	public SeatDetailFrame(SeatBoardPanel parent, SeatDTO seatDTO, Long employeeId) {
		super(CaptionMessages.SEAT_DETAIL_TITLE);
		this.employeeId = employeeId;
		this.seat = seatDTO;
		this.parent = parent;
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				disposeFrame();
				refreshParentGUI();
			}
		});
		super.setSize(800, 500);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		JPanel pnlNavigate = new JPanel();
		getContentPane().add(pnlNavigate, BorderLayout.WEST);
		pnlNavigate.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JToolBar tbrNavigate = new JToolBar();
		tbrNavigate.setOrientation(SwingConstants.VERTICAL);
		pnlNavigate.add(tbrNavigate);
		tbrNavigate.setFloatable(false);
		
		JButton btnProductMenu = new JButton("");
		btnProductMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (splpnlContent.getLeftComponent() == null) {
					splpnlContent.setLeftComponent(pnlScrollMenuContent);
				} else {
					splpnlContent.setLeftComponent(null);
				}
			}
		});
		btnProductMenu.setIcon(new ImageIcon(SeatDetailFrame.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size16/menu-16.png")));
		tbrNavigate.add(btnProductMenu);
		
		pnlMenuContent = new MenuProductPanel(this, seatDTO, employeeId);
		pnlMenuContent.setSize(100, 1000);
		pnlSeatDetail = new SeatDetailPanel(this, seatDTO);
		
		pnlScrollMenuContent = new JScrollPane(pnlMenuContent);
		pnlScrollSeatFilter = new JScrollPane(pnlSeatDetail);
		
		splpnlContent = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pnlScrollMenuContent, pnlSeatDetail);
		getContentPane().add(splpnlContent, BorderLayout.CENTER);
		
		JToolBar tbrActions = new JToolBar();
		
		Panel pnlTitle = new Panel();
		JSplitPane splpnlTop = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pnlTitle, tbrActions);
		
		JButton btnSave = new JButton("");
		btnSave.setToolTipText("Edit Order Line");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				editOrderLine();
			}
		});
		btnSave.setIcon(new ImageIcon(SeatDetailFrame.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/edit-24.png")));
		tbrActions.add(btnSave);
		
		JButton btnPayment = new JButton("");
		btnPayment.setToolTipText("Payment");
		btnPayment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showPaymentFrame();
			}
		});
		btnPayment.setIcon(new ImageIcon(SeatDetailFrame.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/pay-24.png")));
		tbrActions.add(btnPayment);
		
		JButton btnCancelOrder = new JButton("");
		btnCancelOrder.setToolTipText("Cancel Order");
		btnCancelOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (SwingFactory.showConfirmDialog("Cancel the order... Are you sure?")) {
					OrderBusiness.cancelOrder(seat.getId());
					refreshParentGUI();
					disposeFrame();
				}
			}
		});
		btnCancelOrder.setIcon(new ImageIcon(SeatDetailFrame.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/cancel-24.png")));
		tbrActions.add(btnCancelOrder);
		
		
		
		JLabel lblSeatDetail = SwingFactory.createTitleLabel("Seat Detail");
		pnlTitle.add(lblSeatDetail);
		getContentPane().add(splpnlTop, BorderLayout.NORTH);
	}
	
	private void showPaymentFrame() {
		PaymentFrame paymentFrame = new PaymentFrame(parent, this, seat.getId());
		paymentFrame.setVisible(Boolean.TRUE);
	}
	
	public void refreshParentGUI() {
		pnlSeatDetail.refreshGUI();
		parent.refreshGuiAndParent();
	}
	
	private void editOrderLine() {
		EditOrderLineFrame frmEditOrderLine = new EditOrderLineFrame(this, pnlSeatDetail.getOrderLineId());
		frmEditOrderLine.setVisible(Boolean.TRUE);
	}
	
	
	private void switchView(int viewId) {
		if (FILTER_VIEW == viewId) {
			splpnlContent.setLeftComponent(pnlScrollSeatFilter);
		} else if (MENU_VIEW == viewId) {
			splpnlContent.setLeftComponent(pnlScrollMenuContent);
		}
		refreshGUI();
	}
	
}
